

var treeLoader = new Ext.tree.TreeLoader({
	dataUrl:ServerUrl+'fileManager/getDirTree'
});

var rootNode = new Ext.tree.AsyncTreeNode({
	text: 'Root',
	id:'0'
});

//var FolderTree = 
var store = new Ext.data.Store({
	url:ServerUrl+'fileManager/getFileAndFolder',
	autoLoad:true, 
	reader: new Ext.data.JsonReader({
		root:'rows'
	}, [
		{name:'dir_id',type:'int'},
		{name:'file_id',type:'int'},
		{name: 'file_name',type:'string'},
		{name: 'file_size'},
		{name: 'file_type', type: 'string'},
		{name: 'file_modify_time'}
	])
});
//var FolderGrid = 


MyDesktop.FileManager = Ext.extend(Ext.app.Module, {
	id:'1',
    init : function(){
        this.launcher = {
            text: '文件管理器',
            iconCls:'bogus',
            handler : this.createWindow,
            scope: this
        }
    },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('1');
	    win = desktop.createWindow({
	    	title:'manager flie',
	        width:640,
	        height:480,
	        //html : '<p>Something useful would be in here.</p>',
	        iconCls: 'bogus',
	       // shim:false,
	       // animCollapse:false,
	        //layout: 'border',
	        //constrainHeader:true,
	        layout:'border',
	        id:'1',
	        collapsible :true,
	        border:true,
	       // items : [FolderTree,FolderGrid]
			items:[
				new Ext.tree.TreePanel({
					loader: new Ext.tree.TreeLoader({
						dataUrl:ServerUrl+'fileManager/getDirTree',
					}),
					root: new Ext.tree.AsyncTreeNode({
             			text:'文件夹',
             			id:'0',
             			draggable:false 
             		}),
					region : 'west',
					frame : true,
					width : 200,
					title:'我的文件夹',
					rootVisible:false,
					autoHeight : false,
					border : true, // 边框
					animate : true, // 动画效果
					frame : true,// 美化界面
					autoScroll:true,
					enableDD : true,// 是否支持拖拽效果
					containerScroll : true,// 是否支持滚动条
					selModel: new Ext.tree.MultiSelectionModel(),
					tools:[{
            			id:'refresh',
                		on:{
               				click: function(){
	                    		var tree = Ext.getCmp('1');
	                        	tree.body.mask('Loading', 'x-mask-loading');
	                        	tree.root.reload();
	                       		tree.root.collapse(true, false);
	                        	setTimeout(function(){ // mimic a server call
	                        		tree.body.unmask();
	                            	tree.root.expand(true, true);
	                      		}, 1000);
                			}
             			}
            	 	}],
            	 	tbar:[{
            	 		text:'新建文件',
            	 		tooltip:'rename a file',
                        iconCls:'rename',
            	 		handler:function(node,e){
            	 			//alert(FolderTree.getTreeEl());
            	 			var sm = FolderTree.getSelectionModel().select();
            	 		}	
            	 	},'-',{text:'新建文件夹'}],
            	 	listeners: {
            	 		'click':function(node,e){
            	 			var store = Ext.getCmp('folder').store = new Ext.data.Store({
								url:ServerUrl+'fileManager/getFileAndFolder/'+node.id,
								autoLoad:true, 
								reader: new Ext.data.JsonReader({
									root:'rows'
								}, [
									{name:'dir_id',type:'int'},
									{name:'file_id',type:'int'},
									{name: 'file_name',type:'string'},
									{name: 'file_size'},
									{name: 'file_type', type: 'string'},
									{name: 'file_modify_time'}
								  ])
								}); 
							Ext.getCmp('folder').reconfigure(store,Ext.getCmp('folder').getColumnModel());
            	 		}
            	 	}
				}),		
				new Ext.grid.GridPanel({  
		            id:'folder',
                	region:'center',     
            		border:false, 
		            store:store,
		            //ds: , 
		   			cm: new Ext.grid.ColumnModel([
                        new Ext.grid.RowNumberer(),
                        {header:'DirID',dataIndex: 'dir_id',hidden:true},        
                        {header: "ID", dataIndex: 'file_id', hidden: true},
                        {header: "名称", width: 120, sortable: true, dataIndex: 'file_name'},
                        {header: "大小", width: 70, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: 'file_size'},
                        {header: "类型", width: 70, sortable: true, dataIndex: 'file_type'},
                        {header: "修改时间", width: 70, sortable: true, dataIndex: 'file_modify_time'}
                    ]),	
                    viewConfig: {
                         forceFit:true
                    },
                    tbar:[{
                            text:'新建文件',
                            tooltip:'Add a new file',
                            iconCls:'add',
                            handler:function(){
                            	Ext.Msg.prompt('新建文件','请输入新文件名',function(btn,text){
                            		if(btn == 'ok' && text) {
                            			
                            		}
                            	},this, false, '');
                        
                            }
                        }, '-', {
                            text:'重命名',
                            tooltip:'rename a file',
                            iconCls:'rename',
                            handler:function(){
                            	var sm = Ext.getCmp('folder').getSelectionModel();
                            	if (sm.hasSelection()){
                            		var sel = sm.getSelected();
                            		var DirID = sel.data.dir_id;
                            		Ext.Msg.prompt('重命名文件','请输入新文件名',
                            			function(btn,text){
                            				if(btn == 'ok' && text) {
	                            				var FileID = sel.data.file_id;
	                            				var FileOldName = sel.data.file_name;
	                            				Ext.Ajax.request({
	                            					url:ServerUrl+'fileManager/renameFileName',
													method:'POST',
													params: { file_id : FileID, file_name : text,file_old_name :FileOldName,dir_id:DirID },	
													success:function(response){
														var r = Ext.util.JSON.decode(response.responseText);
														if(r.msg == 'file_exist') {
															Ext.Msg.show({
																title:'警告',
																width:300,
																buttons:Ext.MessageBox.OK,
																msg:'文件名已经存在！',
																icon: Ext.MessageBox.WARNING
															});
														} else if(r.msg == 'file_ext_not_valid') {
															Ext.Msg.show({
																title:'警告',
																width:300,
																buttons:Ext.MessageBox.OK,
																msg:'文件扩展名不合法！',
																icon: Ext.MessageBox.WARNING
															});
														} else if(r.msg == 'ok'){
															sel.set('file_name',r.text);
														}
													}
	                            				});
                            				}
                            			}
                            		,this, false, '');
                            	}
                            }
                        },'-',{
                            text:'删除',
                            tooltip:'remove a file',
                            iconCls:'remove',
                           	handler: function(){
								var sm = Ext.getCmp('folder').getSelectionModel();
								if (sm.hasSelection()){
									var sel = sm.getSelected();
									Ext.Msg.show({
										title: '确认删除文件',
										buttons: Ext.MessageBox.YESNO,
										width:300,
										msg: '确定要删除 '+sel.data.file_name+'文件吗?',
										fn: function(btn,text){
											if (btn == 'yes'){
												var FileID = sel.data.file_id
												Ext.Ajax.request({
													url:ServerUrl+'fileManager/removeTheFile',
													method:'POST',
													params: { file_id : FileID },
													success:function(response){
														if(response.responseText) {
															Ext.getCmp('folder').getStore().remove(sel);
														} else {
															Ext.Msg.show({
																title:'错误提示',
																width:300,
																buttons:Ext.MessageBox.OK,
																msg:'删除时发生错误',
																icon: Ext.MessageBox.ERROR
															});
															 
														}
													}
												});
												//alert();
												//sel.set('title', text);
												
											}
										}
									});
								}
							}
                    }],
		        })	
			
			]
         });
        
        win.show();
		if(win) {
			updateAppStatues('1','on');
		}

    }
});



